package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zhangjunfei
 * @date 2023/8/7 13:46
 */
public class L_2146 {
    public int[] sortEvenOdd(int[] nums) {
        int x=0;
        if (nums.length<=2){
            return nums;
        }
        List<Integer> oddList=new ArrayList<>();
        List<Integer> evenList=new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (i%2==0){
                evenList.add(nums[i]);
            }else {
                oddList.add(nums[i]);
            }
        }
        Collections.sort(evenList);
        Collections.sort(oddList);
        int size = oddList.size() - 1;
        for (int i = 0; i < nums.length; i++) {
            if (i%2==0){
                nums[i]=evenList.get(x++);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (i%2!=0){
                nums[i]=oddList.get(size--);
            }
        }
        return nums;
    }
}
